// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Winamp.cs" company="GT">
//   Gunther Taborga
// </copyright>
// <summary>
//   Defines the Winamp type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

// TODO refactored=true
namespace BatchID3.Editor.Plugins
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Windows;

    using BatchID3.FileSystem;
    using BatchID3.Mp3;

    using Microsoft.Win32;

    public class Winamp : IWinamp
    {
        private const string WinampRegistryKey = @"Software\Winamp";

        private const string WinampNotInstalledMessage = "Cannot enqueue song(s) because Winamp is not installed on this computer.";

        private const string WinampNotInstalledCaption = "Error";

        private const string PlayOnLoadFlag = "/ADD";

        private const string FileFormatString = " \"{0}\"";

        private readonly IFileSystemManager fileSystemManager;

        public Winamp(IFileSystemManager fileSystemManager)
        {
            if (fileSystemManager == null)
            {
                throw new ArgumentNullException("fileSystemManager");
            }

            this.fileSystemManager = fileSystemManager;
        }

        public void Enqueue(IEnumerable<IMp3Entity> mp3Entities)
        {
            this.Start(mp3Entities);
        }

        public void Play(IEnumerable<IMp3Entity> mp3Entities)
        {
            this.Start(mp3Entities, true);
        }

        private static RegistryKey GetWinampRegistryKey()
        {            
            return Registry.CurrentUser.OpenSubKey(WinampRegistryKey);
        }

        private void Start(IEnumerable<IMp3Entity> mp3Entities, bool playOnLoad = false)
        {
            var mp3List = new List<IMp3Entity>(mp3Entities);

            if (!mp3List.Any())
            {
                return;
            }

            var winampKey = GetWinampRegistryKey();

            if (winampKey == null)
            {
                System.Windows.MessageBox.Show(
                    WinampNotInstalledMessage,
                    WinampNotInstalledCaption,
                    MessageBoxButton.OK,
                    MessageBoxImage.Exclamation);

                return;
            }

            var winampFilePath = winampKey.GetValue(string.Empty) as string;

            if (string.IsNullOrEmpty(winampFilePath))
            {
                return;
            }

            var commandLineBuilder = new StringBuilder();

            if (!playOnLoad)
            {
                commandLineBuilder.Append(PlayOnLoadFlag);
            }

            foreach (var mp3 in mp3List)
            {
                commandLineBuilder.AppendFormat(FileFormatString, mp3.FilePath);
            }

            Process.Start(this.GetWinampProcessString(winampFilePath), commandLineBuilder.ToString());
        }

        private string GetWinampProcessString(string winampFilePath)
        {
            return string.Format("{0}{1}winamp.exe", winampFilePath, this.fileSystemManager.DirectorySeparatorChar);
        }
    }
}